package com.xoa.service.emergency;

import com.xoa.dao.emergency.CpAssetTypeMapper;
import com.xoa.model.emergency.CpAssetType;
import com.xoa.util.LimsJson;
import com.xoa.util.ToJson;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @作者 杨岩林
 * @创建日期 16:532019/10/15
 * @类介绍
 */
@Service
public class CpAssetTypeService {

    @Resource
    private CpAssetTypeMapper cpAssetTypeMapper;

    public LimsJson queryYjType() {
        LimsJson toJson = new LimsJson(1, "err");
        List<CpAssetType> cpAssetTypes=cpAssetTypeMapper.selectTypeYj();
        if(cpAssetTypes!=null && cpAssetTypes.size()>0){
            for(CpAssetType cpAssetType:cpAssetTypes){
                cpAssetType.setLabel(cpAssetType.getTypeName());
                int i = cpAssetTypeMapper.selectTypeCountByParent(cpAssetType.getTypeId());
                if(i>0){
                    cpAssetType.setIsLeaf(false);
                } else {
                    cpAssetType.setIsLeaf(true);
                }

            }
            toJson.setCode(0);
            toJson.setMsg("查询成功");
            toJson.setData(cpAssetTypes);
        }

        return toJson;
    }

    public LimsJson queryChild(Integer parentId) {
        LimsJson toJson = new LimsJson(1, "err");
        List<CpAssetType> cpAssetTypeList=cpAssetTypeMapper.selectTypeZj(parentId);
        if(cpAssetTypeList!=null&& cpAssetTypeList.size()>0){
            for(CpAssetType cpAssetType:cpAssetTypeList){
                if(cpAssetType.getTypeNum()<=3){
                    cpAssetType.setIsLeaf(false);
                    cpAssetType.setLabel(cpAssetType.getTypeName());
                }else{
                    cpAssetType.setIsLeaf(true);
                    cpAssetType.setLabel(cpAssetType.getTypeName());
                }

            }
            toJson.setCode(0);
            toJson.setMsg("查询成功");
            toJson.setData(cpAssetTypeList);
        }
        return toJson;
    }

    public ToJson insertCpType(CpAssetType cpAssetType) {
        ToJson toJson =new ToJson(1,"err");
        try{
            int i = cpAssetTypeMapper.insertSelective(cpAssetType);
            if(i>0){
                toJson.setFlag(0);
                toJson.setMsg("true");
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }
    public ToJson delCpType(Integer typeId) {
        ToJson toJson =new ToJson(1,"err");
        try{
            int i = cpAssetTypeMapper.deleteByPrimaryKey(typeId);
            if(i>0){
                toJson.setFlag(0);
                toJson.setMsg("true");
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

}
